//
//  TableHeaderView.m
//  lizhong
//
//  Created by 李忠 on 4/28/16.
//  Copyright © 2016 李忠. All rights reserved.
//

#import "SDCycleAdvertView.h"
#import "UIView+SDExtension.h"

#define kWidth (self.frame.size.width)
#define kHeight (self.frame.size.height)
#define kArrCount (self.imageArray.count)

@interface SDCycleAdvertView()<UIScrollViewDelegate>

@property (weak, nonatomic) UIScrollView *scrollView;

@property (weak, nonatomic) UIPageControl *pageControll;
@property (strong, nonatomic) NSTimer *timer;

@end

@implementation SDCycleAdvertView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
    }
    return self;
}



- (void)setupUI{
    //设置scrollView
    //启用scrollView的分页效果
   
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
    
    
    self.scrollView = scroll;
    self.scrollView.pagingEnabled = YES;
    //关闭垂直滚动
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //关闭弹簧效果
    self.scrollView.bounces = NO;
    
    //设置pageControll
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kHeight*0.8, kWidth, 20)];
    page.sdX = self.center.x;
    self.pageControll = page;
    self.pageControll.numberOfPages = 6;
    self.pageControll.currentPage = 0;
    self.pageControll.pageIndicatorTintColor = [UIColor yellowColor];
    self.pageControll.currentPageIndicatorTintColor = [UIColor redColor];
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControll];
}

- (void)setImageArray:(NSArray *)imageArray{
    _imageArray = imageArray;
    self.scrollView.contentSize = CGSizeMake((kArrCount+2)*kWidth, 0);
    self.pageControll.numberOfPages = kArrCount;
    [self setupScroll];
    [self setupTimer];
}

- (void)setupTimer{

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextpage) userInfo:nil repeats:YES];

    
}

- (void)nextpage{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x+kWidth, 0) animated:1];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self moveOffset];
}

//在拖拽的时候停止
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
}

//在停止拖拽的时候开启定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self setupTimer];
}

- (void)setupScroll{
    
    self.pageControll.numberOfPages = kArrCount;
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    image1.image = [UIImage imageNamed:self.imageArray.lastObject];
    [self.scrollView addSubview:image1];
    self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    
    for (int i = 0; i < kArrCount; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth*(i+1), 0, kWidth, kHeight)];
        
        image.image = [UIImage imageNamed:self.imageArray[i]];
        
        [self.scrollView addSubview:image];

    }
    
    UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth*(kArrCount+1), 0, kWidth, kHeight)];
    image2.image = [UIImage imageNamed:self.imageArray.firstObject];
    [self.scrollView addSubview:image2];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self moveOffset];
}

//核心移动偏移量
- (void)moveOffset{
    if (self.scrollView.contentOffset.x >= kWidth * (kArrCount + 1)) {
        self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    }else if(self.scrollView.contentOffset.x <= 0){
        self.scrollView.contentOffset = CGPointMake(kWidth * kArrCount, 0);
    }
}

//计算页码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger index = round(self.scrollView.contentOffset.x / kWidth -1);
    if (index == kArrCount) {
        index = 0;
    }else if(index == -1){
        index = kArrCount -1;
    }
    self.pageControll.currentPage = index;
}

@end
